﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10
{
    class OrderItem
    {
        public string INa;
        public double Price;
        public int Amount;
        public double TotalPrice;

        public OrderItem(double OPrice, int OAmount, string OName)
        {
            INa = OName;
            Price = OPrice;
            Amount = OAmount;
            TotalPrice = Price * Amount;
        }
    }
    
    class Order
    {
        public string CNa;
        public double Sum;
        public int ID;
        public List<OrderItem> ItemList = new List<OrderItem>();

        public Order(int OID, string CName)
        {
            ID = OID;
            CNa = CName;
        }
        
        public override bool Equals(object obj)
        {
            Order o = obj as Order;
            return o != null && o.ID == ID;
        }

        public string alterCName(string newName)
        {
            CNa = newName;
            return CNa;
        }
        
        public void addOrderItem(OrderItem item)
        {
            ItemList.Add(item);
            Console.WriteLine("订单明细添加成功："+item.INa);
            Sum += item.TotalPrice;
        }
        
        public override string ToString()
        {
            Console.WriteLine("---------------------------------------------------------------------");
            Console.WriteLine("订单号：" + ID + ' ' + "客户名：" + CNa);
            foreach (OrderItem a in ItemList)
            {
                Console.WriteLine("商品名称：" + a.INa + ' ' + "商品单价：" + a.Price + ' ' + "购买数量：" + a.Amount + ' ' + "总价：" + a.TotalPrice + ' ');
            }
            Console.WriteLine("总额：" + Sum);
            Console.WriteLine("---------------------------------------------------------------------");
            Console.WriteLine();
            return null;
        }
    }
    
    class OrderService
    {
        public List<Order> ItemList = new List<Order>();
        //public List<OrderItem> ItemList1 = new List<OrderItem>();
        public bool searchOrder(int ID)
        {
            var order = from w in ItemList where w.ID == ID select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("订单号为" + ID + "的订单不存在");
                Console.WriteLine();
                return false;
            }
            Console.WriteLine("订单号为" + ID + "的订单已找到");
            order.FirstOrDefault().ToString();
            return true;
        }

        public bool searchOrder(string name)
        {
            var order = from w in ItemList where w.CNa == name select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("用户名为" + name + "的订单不存在");
                Console.WriteLine();
                return false;
            }
            Console.WriteLine("用户：" + name + "的订单已找到");
            order.FirstOrDefault().ToString();
            return true;
        }
        /*
        public bool searchOrderByIName(string item)
        {
            var order = from w in ItemList1 where w.INa == item select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("含有商品" + item + "的订单不存在");
                Console.WriteLine();
                return false;
            }
            Console.WriteLine("含有商品" + item + "的订单已找到");
            order.FirstOrDefault().ToString();
            return true;
        }
        */
        public void addOrder(Order a)
        {
            bool flag = true;
            foreach (Order order in ItemList)
            {
                if (a.Equals(order))
                {
                    Console.WriteLine("订单号为"+a.ID+"的订单已存在，无法再次添加");
                    Console.WriteLine();
                    flag = false;
                    break;
                }
            }
            if (flag == true)
            {
                ItemList.Add(a);
                Console.WriteLine("订单添加成功，订单号："+a.ID);
            }
        }
        
        public void deleteOrder(int ID)
        {
            var order = from w in ItemList where w.ID == ID select w;
            if (order.FirstOrDefault() != null)
            {
                ItemList.Remove(order.FirstOrDefault());
                Console.WriteLine("订单号为："+ID+ "的订单删除成功");
            }
            else
                Console.WriteLine("订单号为"+ID+"的订单不存在");
            Console.WriteLine();
        }
        
        public void AlterOrder(int ID)
        {
            var order = from w in ItemList where w.ID == ID select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("订单号为" + ID + "的订单不存在");
                Console.WriteLine();
            }
            else
                Console.WriteLine("请输入您的新用户名：");
                string str = Console.ReadLine();
                order.FirstOrDefault().alterCName(str);
            Console.WriteLine("修改成功，修改后的订单：");
            order.FirstOrDefault().ToString();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();
            Console.WriteLine("测试添加订单明细：");
            Console.WriteLine();
            Order firstOrder = new Order(2020001, "王二");
            OrderItem pineapple = new OrderItem(5, 4, "菠萝");
            firstOrder.addOrderItem(pineapple);
            OrderItem apple = new OrderItem(3, 20, "苹果");
            firstOrder.addOrderItem(apple);
            OrderItem banana = new OrderItem(5, 4, "香蕉");
            firstOrder.addOrderItem(banana);
            Order secondOrder = new Order(2020002, "法外狂徒张三");
            OrderItem doufu = new OrderItem(2, 3, "豆腐");
            secondOrder.addOrderItem(doufu);
            OrderItem noodles = new OrderItem(1.5, 5, "面条");
            secondOrder.addOrderItem(noodles);
            Order thirdOrder = new Order(2020003, "李四");
            OrderItem mlxg = new OrderItem(34, 1, "麻辣香锅");
            thirdOrder.addOrderItem(mlxg);
            OrderItem mlt = new OrderItem(27, 1, "麻辣烫");
            thirdOrder.addOrderItem(mlt);
            OrderItem hotpot = new OrderItem(288, 1, "火锅");
            thirdOrder.addOrderItem(hotpot);
            Order fourthOrder = new Order(2020001, "王二");
            Console.WriteLine();
            Console.WriteLine("测试添加订单：");
            Console.WriteLine();
            service.addOrder(firstOrder);
            service.addOrder(secondOrder);
            service.addOrder(thirdOrder);
            service.addOrder(fourthOrder);
            Console.WriteLine("测试查找订单：");
            Console.WriteLine();
            service.searchOrder(2020001);
            service.searchOrder(2020002);
            service.searchOrder(2020003);
            service.deleteOrder(2020001);
            service.searchOrder(2020001);
            //service.searchOrderByIName("面条");
            service.searchOrder("法外狂徒张三");
            Console.WriteLine("修改订单用户名测试：");
            Console.WriteLine();
            Console.WriteLine("若想要修改某一订单的用户名，请按一下“y”键");
            string str1 = Console.ReadLine();
            string str2 = "y";
            if (str1 == str2)
                service.AlterOrder(2020002);
            else
                Console.WriteLine("未做任何修改");
            Console.ReadKey();
        }
    }
}
